<style scoped src="./index.scss" lang="scss" rel="stylesheet/scss"></style>
<template src="./index.html"></template>
<script>
export default {
  mixins: [Vue.prototype.$vFN.Mixins.Base, Vue.prototype.$vFN.Mixins.Edit],
  components: {
    SelectDataGoods: async () => Vue.extend(await Vue.prototype.$vFN.Components.Base.SelectDataGoods()),
    SelectImageRadio: async () =>
      Vue.extend(await Vue.prototype.$vFN.Components.Base.SelectImageRadio()),
    SelectImageCheckbox: async () =>
      Vue.extend(
        await Vue.prototype.$vFN.Components.Base.SelectImageCheckbox()
      ),
  },
  data() {
    return {
      appWindow: window,
      form: {
        basic:
          {
            // 队长分红是否开启
            agent_bonus: 0,
            // 分红比例模式：0统一比例，1：走队长等级
            bonus_module: 0,
            // 分红比例
            bonus_ratio: 1,
            // 队长内购
            agent_self_bonus: 1,
            // // 是否统计自己
            // statistics_self: 1,
            // 商品模式：默认是否参与
            product_module: 1,
          },
        leader: 
          {
            // 主动申请
            accord_apply: 1,
            // 申请门槛，0：无条件，1：下线总人数，2：下线队长人数，3：总佣金，4：提现总佣金，5：购买指定商品
            accord_condition: 5,
            // 申请表单
            decoration_id: null,
            // 申请门槛-内容：无条件0传空|null，1-2传整数，3-4传数字，5看condition_content---des
            condition_content: [],
            // 是否需要审核，0：不需要審核，1：需要审核
            need_audit: 1,
            // 购买指定商品格式
            apply_set: {
              top_image: "",
              apply_button_text: "",
            },
            // 
          },
        apply_set:
          {
            // 是否显示入口
            entrance_status: 1,
            // 入口名儿
            entrance: null,
            // 规则说明
            rule_note: "",
            // 等级说明:
            level_note: "",
            // 分红订单是否开启
            bonus_order_status: 1,
            // 育人订单是否开启
            educate_order_status: 1,
            // 团队列表是否开启
            team_status: 1,
          }
      },
      // 队长选项
      captain: [],

      // 等级
      levelList: [],
      // 表单数据
      formList: [],

      load: true,

      text: '',
    };
  },
  methods: {
    // 验证 
    validate() {
      return new Promise((resolve, reject) => {
        this.$refs["editForm"].validate((valid) => {
            valid ? resolve(true) : resolve(false)
        });
      });
    },

    // 获取表单
    async getForm() {
      let res = await this.$vFN.Http.post({
        url: "plugin/team-dividend/admin/setting/getCustomFormList",
      }, window);
      this.formList = res.data
      console.log(res.data);
    },

    // 获取等级
    async getLevelList() {
      let res = await this.$vFN.Http.post({
        url: "plugin/team-dividend/admin/level/list",
      }, window);
      this.form.leader.condition_content = [];
      res.data.list.forEach(item => {
        this.form.leader.condition_content.push({
          level_id: item.id,
          name: item.name,
          product: [],
        })
      });
    },

    // 获取队长选项
    async getCaptain() {
      let res = await this.$vFN.Http.post({
        url: "plugin/team-dividend/admin/setting/getApplySelect",
      }, window);
      console.log(res.data);
      this.captain = res.data;
    },
    async sum() {
      if (!await this.validate()) {
        return
      }
      // if(this.form.leader.condition_content == 1) {
      //   if (!(/(^[0-9]\d*$)/.test(this.form.leader.condition_content))){
      //     this.$vFN.Tools.messageError("下线总人数，请输入正整数");
      //     return;
      //   }
      //   this.form.leader.condition_content = parseInt(this.form.leader.condition_content)
      // }
      // if(this.form.leader.condition_content == 2) {
      //   if (!(/(^[0-9]\d*$)/.test(this.form.leader.condition_content))){
      //     this.$vFN.Tools.messageError("下线队长人数，请输入正整数");
      //     return;
      //   }
      //   this.form.leader.condition_content = parseInt(this.form.leader.condition_content)
      // }
      // if(this.form.leader.condition_content == 5) {
      //   this.form.leader.condition_content.forEach(item => {
      //     if(item.product.length == 0) {
      //       this.$vFN.Tools.messageError("每个等级指定商品不能为空");
      //       return;
      //     }
      //   });
      // }

      switch(this.form.leader.accord_condition) {
        case 0:
          this.form.leader.condition_content = null;
          break;
        case 1:
          if (!(/(^[0-9]\d*$)/.test(this.form.leader.condition_content))){
            this.$vFN.Tools.messageError("下线总人数，请输入正整数");
            return;
          }
          this.form.leader.condition_content = parseInt(this.form.leader.condition_content)
          break;
        case 2:
          if (!(/(^[0-9]\d*$)/.test(this.form.leader.condition_content))){
            this.$vFN.Tools.messageError("下线队长人数，请输入正整数");
            return;
          }
          this.form.leader.condition_content = parseInt(this.form.leader.condition_content)
          break;
        case 5:
          for(let i = 0;i < this.form.leader.condition_content.length;i ++) {
            if(this.form.leader.condition_content[i].product.length == 0) {
              this.$vFN.Tools.messageError("对应等级-指定商品 不能为空");
              return;
            }
          }
          break;
      } 
      
      this.load = true;
      let res = await this.$vFN.Http.post({
          url: "plugin/team-dividend/admin/setting/setBase",
          data: this.form,
        },window);
      if(res.code == 200) {
        this.$vFN.Tools.messageSuccess("操作成功");
        this.getData();
      }
    },

    async getData(change) {
      let res = await this.$vFN.Http.post({
        url: "plugin/team-dividend/admin/setting/getBase",
      },window);
      if(res.data.basic_set != null) {
        this.form = res.data.basic_set;
      }
      
      
      
      this.load = false;
    },

    change() {
      console.log(this.form.leader.accord_condition);
      // if(this.form.leader.accord_condition == 0) {
      //   this.form.leader.condition_content = null;
      // }
      // if(this.form.leader.accord_condition != 0 || this.form.leader.accord_condition != 5) {
      //   this.form.leader.condition_content = 0;
      // }
      // if(this.form.leader.accord_condition == 5) {
      //   this.getLevelList();
      //   this.form.leader.accord_condition == 5
      // }

      switch(this.form.leader.accord_condition) {
        case 0:
          this.form.leader.condition_content = null;
          break;
        case 1:
          this.form.leader.condition_content = 0;
          this.text = '下线总人数';
          break;
        case 2:
          this.form.leader.condition_content = 0;
          this.text = '下线队长人数';
          break;
        case 3:
          this.form.leader.condition_content = 0;
          this.text = '累计佣金';
          break;
        case 4:
          this.form.leader.condition_content = 0;
          this.text = '提现佣金总额';
          break;
        case 5:
          this.getLevelList();
          this.form.leader.accord_condition == 5
          break;
      }

    },

    // 初始化
    async init() {
      this.getData();
      this.getCaptain();
      this.getForm();
    },
  },
  mounted() {
    this.init();
  },
  activated() {},
};
</script>